﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Upreader.Usenet.Nntp
{
    class StatisticsStream : StreamWrapper
    {
        #region Fields

        private readonly INntpStatisticsSource _statisticsSource;

        #endregion // Fields

        #region Constructor

        public StatisticsStream(Stream baseStream, INntpStatisticsSource statisticsSource)
            : base(baseStream)
        {
            if (statisticsSource == null)
                throw new ArgumentNullException("statisticsSource");

            _statisticsSource = statisticsSource;
        }

        #endregion // Constructor

        #region Methods

        public override int Read(byte[] buffer, int offset, int count)
        {
            int total = base.Read(buffer, offset, count);

            _statisticsSource.UpdateTotalBytesReceived(total);

            return total;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            base.Write(buffer, offset, count);

            _statisticsSource.UpdateTotalBytesSent(count);
        }

        #endregion // Methods
    }
}
